// miniprogram/pages/map/map.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    markers: [{
      latitude: 39.908748,
      longitude: 116.397596,
      name: "",
      iconPath: '/images/site.png',
      width: 30,
      height: 30
    }],
    latitude: 39.908748,
    longitude: 116.397596,
    province: '定位'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {

  },

  chooseLocation: function () {
    var that = this;
    wx.chooseLocation({
      success: function (res) {
        const pages = getCurrentPages();
        console.log(pages)
        const prevPage = pages[pages.length - 2]
        prevPage.setData({
          province: res.address.substring(6, 9)
        })
        // console.log(res);
        that.setData({
          markers: [{
            latitude: res.latitude,
            longitude: res.longitude,
            name: res.name,
            iconPath: '/images/site.png',
            width: 30,
            height: 30
          }]
        });
        wx.navigateBack({
          delta: 1
        })
      }
    })
  },

  getLocation: function () {
    var that = this;
    var lat = this.data.latitude;
    var lng = this.data.longitude;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        console.log(res)
        lat = res.latitude;
        lng = res.longitude;
        var locationString = res.latitude + "," + res.longitude;
        wx.request({
          url: 'http://apis.map.qq.com/ws/geocoder/v1/?l&get_poi=1',
          data: {
            "key": "EAIBZ-QK4CR-NWCW5-WRJEX-PDP4O-XGBJK",
            "location": locationString
          },
          method: 'Get',
          success: function (res) {
            that.setData({
              'address': res.data.result.address
            });
            const this_pages = getCurrentPages();
            console.log(this_pages)
            const this_prevPage = this_pages[this_pages.length - 1]
            this_prevPage.setData({
              province: that.data.address.substring(6, 9)
            })
            console.log("请求成功: res" + JSON.stringify(res))
            console.log("请求成功: res.address" + that.data.address.substring(6, 9))
          }
        })

        that.setData({
          markers: [{
            latitude: lat,
            longitude: lng,
            iconPath: '/images/site.png',
            width: 30,
            height: 30
          }]
        });

        wx.navigateBack({
          delta: 1
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})